const express = require('express')
const app = express()
let httpPort = 7788
let httpServer = null
let wwwroot = 'C:\\Inetpub\\wwwroot'

/**
 * 创建websocket服务器
 * @param {Number} options/port 端口号
 * @param {String} options/wwwroot 网站根目录
 */
 const createServer = (options = {}) => {
  // 路由配置 - 启动服务
  console.log('options:', options)
  app.use('/static', express.static(wwwroot))
  httpServer = app.listen(httpPort)
}

const start = (options = {}) => {
  options = options || {}
  httpPort = options.port || httpPort
  wwwroot = options.wwwroot || wwwroot
  createServer(options)
}

const stop = () => {
  // 关闭http服务器
  httpServer && httpServer.close()
}

// 获取服务器对象
const getServer = () => {
  return httpServer
}

module.exports = {
  getServer,
  start,
  stop
}
